/***************************************************************************
*   Copyright (C) 2006 by Yuri Ovcharenko                                 *
*   amwsoft@gmail.com                                                     *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "runnerwidget.h"

#include <QtGui>

RunnerWidget::RunnerWidget( QWidget *parent )
		: QFrame( parent )
{
	QVBoxLayout *lv, *v;
	QHBoxLayout *lh;
	QGroupBox *gbox;

	connect(&process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
	connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
	connect(&process, SIGNAL(readyReadStandardError()), this, SLOT(processReadStdErr()));
	connect(&process, SIGNAL(readyReadStandardOutput()), this, SLOT(processReadStdOut()));

	lv = new QVBoxLayout(this);

	gbox = new QGroupBox(tr("Program to run:"));
	lv->addWidget(gbox);

	v = new QVBoxLayout(gbox);
	lh = new QHBoxLayout();
	v->addLayout(lh);

	leFileName = new QLineEdit();
	lh->addWidget(leFileName);

	bBrowse = new QPushButton(tr("Browse"));
	connect(bBrowse, SIGNAL(clicked()), this, SLOT(getProgName()));
	lh->addWidget(bBrowse);

	gbox = new QGroupBox(tr("Command line"));
	lv->addWidget(gbox);

	v = new QVBoxLayout(gbox);

	leParams = new QLineEdit();
	v->addWidget(leParams);

	gbox = new QGroupBox(tr("Options"));
	lv->addWidget(gbox);

	v = new QVBoxLayout(gbox);

	cbCloseOnRun = new QCheckBox(tr("Close port before start program"));
	v->addWidget(cbCloseOnRun);

	cbAutoReopen = new QCheckBox(tr("Reopen port after program exit"));
	v->addWidget(cbAutoReopen);

	lh = new QHBoxLayout();
	lv->addLayout(lh);
	lh->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Fixed));

	bRun = new QPushButton(tr("Run"));
	connect(bRun, SIGNAL(clicked()), this, SLOT(run()));
	lh->addWidget(bRun);

	logList = new QListWidget();
	lv->addWidget(logList);
}

RunnerWidget::~RunnerWidget()
{
}

void RunnerWidget::saveSettings(QSettings * settings)
{
	settings->setValue("Program/Path", leFileName->text());
	settings->setValue("Program/Options", leParams->text());
	settings->setValue("Port/CloseOnRun", cbCloseOnRun->isChecked());
	settings->setValue("Port/AutoReopen", cbAutoReopen->isChecked());
}

void RunnerWidget::restoreSettings(QSettings * settings)
{
	leFileName->setText(settings->value("Program/Path", QString()).toString());
	leParams->setText(settings->value("Program/Options", QString()).toString());
	cbCloseOnRun->setChecked(settings->value("Port/CloseOnRun", true).toBool());
	cbAutoReopen->setChecked(settings->value("Port/AutoReopen", true).toBool());
}

void RunnerWidget::getProgName()
{
	QString s = QFileDialog::getOpenFileName(this,
						 tr("Select program to run"),
						 leFileName->text(),
						 tr("All files (*)"),
						 0,
						 QFileDialog::DontResolveSymlinks
						);
	if (!s.isEmpty())
		leFileName->setText(s);
}

void RunnerWidget::log(const QString & msg)
{
	QListWidgetItem *item = new QListWidgetItem(msg, logList);
	logList->setCurrentItem(item);
	logList->scrollToItem(item);
}

void RunnerWidget::run()
{
	if (leFileName->text().isEmpty())
		return;

	logList->clear();
	log("Prepare to start");
	emit atStart(cbCloseOnRun->isChecked());

	QStringList args = leParams->text().split(" ", QString::SkipEmptyParts);
	process.start(leFileName->text(), args);
}

void RunnerWidget::processError(QProcess::ProcessError error)
{
	log(QString("ERROR: Can not start process: %1").arg((int)error));
}

void RunnerWidget::processFinished(int exitCode, QProcess::ExitStatus status)
{
	log(QString("Finished: Exit code: %1 status: %2").arg(exitCode).arg((int)status));
	emit stopped(cbAutoReopen->isChecked());
}

void RunnerWidget::processReadStdErr()
{
	QStringList strlist = QString(process.readAllStandardError()).split("\n");
	for (int i = 0; i < strlist.count(); i++)
		log(QString("ERR: %1").arg(strlist.at(i)));
}

void RunnerWidget::processReadStdOut()
{
	QStringList strlist = QString(process.readAllStandardOutput()).split("\n");
	for (int i = 0; i < strlist.count(); i++)
		log(QString("OUT: %1").arg(strlist.at(i)));
}

/* End of file */
